!function($){
  var dialog = function(params){
    this.defaults = {
        'title':'提示',
        'content':'<div>内容为空</div>',
        'okValue':'确定',
        'cancelValue':'取消',
        'ok' : null,
        'cancel': null,
        'showMask':true,
        'vAlign':'middle',
        'init':null,
        'close':null,
        'maskClose':true
    };
    this.opts = $.extend({},this.defaults, params);
    
    this.outerBox = '';
    this.headBox = '';
    this.innerBox = '';
    this.footerBox = '';
    this.mask = '';
    this.okBtn = '';
    this.cancelBtn = '';
    this.closeBtn = '';
    this.height = '';
    this.width = '';
    this.top = '';
    this.left = '';
    this.dialogInterval = null; 
    
    this._init();
    
    window.aceDialog = this;
  }
  
  $.extend(dialog.prototype, {
    _init : function(){
      if ($(".aceDialog").length > 0) {
        return false;
      }
      this.outerBox = $('<div class="aceDialog popup-common float-div hidden"></div>');
      this.headBox = $('<div class="aceHeader popup-top-common clearfix"></div>');
      this.innerBox = $('<div class="aceContent popup-cont-common"></div>');
      this.footerBox = $('<div class="aceFooter popup-bottom-common clearfix"></div>');
      this.mask = $('<div class="aceMask mask-layer hidden"></div>').click(function(e){
        e.stopPropagation();
        if (aceDialog.opts.maskClose == true) {
          aceDialog.close();
        }
      });;
      this.okBtn = $('<button class="okBtn btn-sure-log right">'+this.opts.okValue+'</button>').click(function(){
        if (aceDialog.opts.ok != null) {
          aceDialog.opts.ok();
        } else {
          aceDialog.close();
        }
      });
      this.cancelBtn = $('<button class="cancelBtn btn-cancel-log right style="margin-right:10px;">'+this.opts.cancelValue+'</button>').click(function(){
        aceDialog.close();
      });
      this.closeBtn = $('<a href="javascript:;" class="btn-close-log right"></a>').click(function(){
        aceDialog.close();
      });
      $('body').append(this.outerBox);
      this.outerBox.append(this.headBox).append(this.innerBox).append(this.footerBox);
      if (this.opts.okValue != '') {
        this.footerBox.append(this.okBtn);
      }
      if (this.opts.cancelValue != '') {
        this.footerBox.append(this.cancelBtn);
      }
      
      this.headBox.append('<span class="fs18 left">'+this.opts.title+'</span>').append(this.closeBtn);
      this.innerBox.append(this.opts.content);
      this.mask.insertBefore(this.outerBox);

      this.height = $(".aceDialog").height();
      this.show();
    },
    show:function(){
        if (this.outerBox.hasClass('hidden')) {
          this.outerBox.css("top", "-800px").removeClass('hidden');
          this.width = $(".aceDialog").width();
          this.height = $(".aceDialog").height();
          this.left = Math.floor(($(document).width()-this.width)/2);
          if (this.left < 0) {
            this.left = 0;
          }
          this.outerBox.removeClass('hidden').css({ "top":"-1000px", "left":this.left+'px' });
          this.showMask();
          if (this.opts.init != null && $('.aceDialog .edui-editor').length <= 0) {
            this.opts.init();
          }
          
          this.height = $(".aceDialog").height();
          this.top = Math.floor(($(window).height()-this.height)/2);
          //console.log($(window).height(), this.height, this.top, 'qian20130603');
          if (this.top < 0) {
            this.top = 0;
          }
          $('.aceDialog').hide().css('top', this.top+'px').show();

          //this.dialogInterval = window.setTimeout(_show, 10);
        }
    },
    close:function(){
      if (!this.outerBox.hasClass('hidden')) {
        this.hideMask();
        this.outerBox.addClass('hidden').remove();
        if (this.opts.close != null) {
          this.opts.close();
        }
        //_close();
      }
    },
    showMask:function()
    {
      this.mask.removeClass('hidden');
    },
    hideMask:function()
    {
      $(".aceMask").hide().remove();
    }
  });
  
  if(!window.ace){
    ace = {};
  }
  $.extend(ace, {
    dialog:function(e){
      return new dialog(e);
    }
  })
}(jQuery);
function _show(){
  var top = 120;
  
  aceDialog.showMask();
  if (aceDialog.opts.init != null && $('.aceDialog .edui-editor').length <= 0) {
    aceDialog.opts.init();
  }
  if (boxTop >= top) {
   clearTimeout(aceDialog.dialogInterval);
   return false;
 }
  
  var boxTop = $('.aceDialog').css('top');
  boxTop = parseFloat(boxTop);

  $('.aceDialog').hide().css('top', top+'px').show();
}
function _close(){
  //this.outerBox.addClass('hidden');
  var boxTop = $('.aceDialog').css('top');
  boxTop = parseFloat(boxTop);
  aceDialog.hideMask();
  $('.aceDialog').addClass('hidden').remove();
}